package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.RubbishBinCurDao;
import cn.topevery.garden.entity.po.RubbishBinCur;
import cn.topevery.garden.entity.query.RubbishBinCurQuery;
import cn.topevery.garden.entity.vo.RubbishBinCurVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 垃圾桶当前状态表
 *
 * @author code-generate
 * @date 2021-09-06
 */
@Repository
public class RubbishBinCurDaoImpl extends BaseDaoImpl<RubbishBinCur> implements RubbishBinCurDao {
    @Override
    public <P extends DbCsp> List<RubbishBinCur> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<RubbishBinCurVO> listPage(RubbishBinCurQuery query) {
        String table = "t_rubbish_bin_cur trbc";

        String columns = "trbc.*";
        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("trbc.c_db_last_update_date desc");
        sqlQueryMate
                .where(true, "trbc.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(query.getTemId()), "trbc.c_device_code", ScriptConditionEnum.Like, query.getTemId());
        return getDataPager(sqlQueryMate, pageSize, pageIndex, RubbishBinCurVO.class);
    }

    @Override
    public RubbishBinCur getByTemId(String temId) {
        String table = "t_rubbish_bin_cur trbc";

        String columns = "trbc.*";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate
                .where(true, "trbc.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "trbc.c_tem_id", ScriptConditionEnum.Eq, temId);
        AtomicReference<RubbishBinCur> binCur = new AtomicReference<>(new RubbishBinCur());
        run(sqlQueryMate.getSql(), CommandType.Text, dbCommand -> {
            sqlQueryMate.getParameters().forEach(dbCommand::addParameter);
            binCur.set(dbCommand.executeToEntity(RubbishBinCur.class));
        });
        return binCur.get();
    }

    @Override
    public RubbishBinCur getEntityByColumn(String key, String value){
        KeyValue<String> map=new KeyValue<>();
        map.key=key;
        map.value=value;
        return getEntityByColumnEqValue(map);
    }
}
